package todo;

import se.lth.cs.realtime.event.RTEvent;
import done.AbstractWashingMachine;

public class WashingProgram1 extends WashingProgram {

	public static final int SPIN_OFF = 0;
	public static final int SPIN_SLOW = 1;
	public static final int SPIN_FAST = 2;
	
	double theSpeed;

	protected WashingProgram1(AbstractWashingMachine mach, double speed,
			TemperatureController tempController,
			WaterController waterController, SpinController spinController) {

		super(mach, speed, tempController, waterController, spinController);
		this.theSpeed = speed;
	}

	// myMachine = mach;
	// mySpeed = speed;
	// myTempController = tempController;
	// myWaterController = waterController;
	// mySpinController = spinController;

	@Override
	protected void wash() throws InterruptedException {

		myMachine.setLock(true);
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_FILL, 0.5));
		
		mailbox.doFetch();//Vänta på full?
		
		System.out.println("Water fill klart");
		
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.0));
		System.out.println("Water idle");
		//mailbox.doFetch();//Vänta på idle
		
		
		mySpinController.putEvent(new SpinEvent(this, 1));
		System.out.println("Spinn på");
		
		myTempController.putEvent(new TemperatureEvent(this, TemperatureEvent.TEMP_SET, 60));
		mailbox.doFetch();
		
		System.out.println("Temp_SET gjord");
		
		
		sleep((long) (18000/theSpeed));//Fel tid just nu
		System.out.println("Done wash part 1");
		// Drain
		System.out.println("Efter tiden");
		
		myTempController.putEvent(new TemperatureEvent(this, TemperatureEvent.TEMP_IDLE, 0));
		mailbox.doFetch();
		System.out.println("Temp idle");
		
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_OFF));
		mailbox.doFetch(); //Funkar inte låser tråden...
		
		System.out.println("Spinn off");
		
		
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_DRAIN, 0));
		mailbox.doFetch();
		
		System.out.println("Water drain");
		
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.0));
		
		
		for (int i = 0; i < 5; i++) {
			System.out.println("Rinsing...");
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_FILL, 0.5));
			mailbox.doFetch();
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.0));
			mailbox.doFetch();
			//Spinn on?
			sleep((long) (12000/theSpeed));//Sov 2 min
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_DRAIN, 0.0));
			mailbox.doFetch(); //Tomt
									
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.0));
			mailbox.doFetch();
						
		}
		System.out.println("Centrifugering gjord");
		
		//Centrifugering
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_FAST));
		sleep((long) (5*60*1000/theSpeed));
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_OFF));
		myMachine.setLock(false);
		System.out.println("Done!");
	}
	
	

	

}









































